/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.edu.sistemaacademico;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import br.edu.sistemaacademico.pojo.Curso;
import br.edu.sistemaacademico.state.StateHandler;
import client.ClientCommand;

public class frmCurso extends FrmModelo{
	Curso curso;
	StateHandler stateAlterarExcuir = new StateHandler(false);
    public List<Object> campos = ClientCommand.getCampos("CursoService"); //new ArrayList<Object>();
    private List<Object> valores = new ArrayList<Object>();
    private List<Object> tamanho = new ArrayList<Object>();
    TabelaModelo tabModel;
    List<Curso> extra;
    
    public frmCurso(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        tamanho.add("80");
        tabModel = new TabelaModelo(campos, tamanho, getJTable());
        tabModel.busca(getTfBuscar());
        setTitle("Curso");
        ListSelectionModel listModel = getJTable().getSelectionModel();
        listModel.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        listModel.addListSelectionListener(list);
        getbtnVoltar().addActionListener(new ActionListener() {
		
			@Override
			public void actionPerformed(ActionEvent e) {
		           	dispose();		
			}
		});
        setButtons();
        setStateAlteraExcuir();
        setButtonAction();
        atualiza();
        setVisible(true);
    }
    public void setButtons(){
        JButton btn4 = getbtn4();
        JButton btn5 = getbtn5();
        JButton btn6 = getbtn6();

        btn4.setVisible(false);
        btn5.setVisible(false);
        btn6.setVisible(false);       
    }
    
    public void atualiza(){
    	((DefaultTableModel) getJTable().getModel()).setNumRows(0);
    	getJTable().updateUI();
    	extra = (List) ClientCommand.carregar("CursoService","codigo = codigo", "");
    	for(int i = 0; i < extra.size();i++ ){
    		valores = new LinkedList<Object>();
    		valores.add(extra.get(i).getId());
    		valores.add(extra.get(i).getNome());
    		valores.add(extra.get(i).getAnoRegulamentado());
    		tabModel.setValores(valores);
    		valores.clear();
    	}
    }
    public void setButtonAction(){
    	getbtnAlterar().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				new frmAtualizaCurso(curso);
				atualiza();
			}
		});
    	getJbtnGravar().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				new frmCadastroCurso();
				atualiza();
			}
		});
    	getbtnExcluir().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(curso!=null)
				ClientCommand.deletar("CursoService", curso);
				else
					JOptionPane.showConfirmDialog(null, "seleciona o curso que deseja excluir");
			}
		});
    }
    ListSelectionListener list = new ListSelectionListener() {
		
		@Override
		public void valueChanged(ListSelectionEvent e) {
			try{
				if(getJTable().getSelectedRow()>=0){
			 curso = getSelectedCurso(Long.parseLong(getJTable().getValueAt(getJTable().getSelectedRow(), 0).toString()));
			 stateAlterarExcuir.change(true);
			 }
			}catch(ArrayIndexOutOfBoundsException ea){
				curso = null;
				stateAlterarExcuir.change(false);
			}
		}
	};	
    public Curso getSelectedCurso(Long codigo){
    	return (Curso) ClientCommand.carregaObjeto("CursoService","codigo ="+ codigo, "");
		}
    public void setStateAlteraExcuir(){
    	stateAlterarExcuir.addComponent(getbtnAlterar());
    	stateAlterarExcuir.addComponent(getbtnExcluir());
    }
    
    
    
}
